package org.sample.project.util;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

public class DatabaseTypeUtil {

    /**
     * 根据Connection对象判断数据库类型
     *
     * @param connection JDBC Connection对象
     * @return 数据库类型（如MySQL、PostgreSQL等）
     * @throws SQLException 如果获取数据库元数据失败
     */
    public static String getDatabaseTypeByConnection(Connection connection) throws SQLException {
        if (connection == null) {
            throw new IllegalArgumentException("Connection cannot be null");
        }

        DatabaseMetaData metaData = connection.getMetaData();
        String databaseProductName = metaData.getDatabaseProductName();

        if (databaseProductName.contains("MySQL")) {
            return "mysql";
        } else if (databaseProductName.contains("DM DBMS")) {
            return "dm";
        } else if (databaseProductName.contains("KingbaseES")) {
            return "kingbase";
        }
        // 海量数据库
        else if (databaseProductName.contains("PostgreSQL")) {
            return "pg";
        } else if (databaseProductName.contains("Microsoft SQL Server")) {
            return "sqlserver";
        } else {
            return "";
        }
    }
}
